package com.stylefeng.guns.modular.purchase.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.IdGenerator;
import com.stylefeng.guns.modular.dto.PurchaseAddRequest;
import com.stylefeng.guns.modular.dto.PurchaseQueryRequest;
import com.stylefeng.guns.modular.purchase.service.IPurchaseGoodsService;
import com.stylefeng.guns.modular.system.model.PurchaseGoods;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.Purchase;
import com.stylefeng.guns.modular.purchase.service.IPurchaseService;

import java.util.Date;


/**
 * 进货入库控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 20:38:54
 */
@Controller
@RequestMapping("/purchase")
public class PurchaseController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(PurchaseController.class);

    private String PREFIX = "/purchase/purchase/";

    @Autowired
    private IPurchaseService purchaseService;
    @Autowired
    private IPurchaseGoodsService purchaseGoodsService;

    /**
     * 跳转到进货入库首页
     */
    @RequestMapping("")
    public String index(Model model) {
        String orderNumber = "JH" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
        model.addAttribute("orderNumber", orderNumber);
        return PREFIX + "purchase.html";
    }

    /**
     * 跳转到添加进货入库
     */
    @RequestMapping("/purchase_add")
    public String purchaseAdd() {
        return PREFIX + "purchase_add.html";
    }

    /**
     * 跳转到修改进货入库
     */
    @RequestMapping("/purchase_update/{purchaseId}")
    public String purchaseUpdate(@PathVariable Integer purchaseId, Model model) {
        Purchase purchase = purchaseService.selectById(purchaseId);
        model.addAttribute("item",purchase);
        LogObjectHolder.me().set(purchase);
        return PREFIX + "purchase_edit.html";
    }

    /**
     * 获取进货入库列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(PurchaseQueryRequest request) {
        Page<Purchase> page = new PageFactory<Purchase>().defaultPage();
        Page<Purchase> resultPage = purchaseService.selectPageWithDataScope(page,request);
        PageInfoBT<Purchase> pageInfoBT = this.packForBT(resultPage);
        return pageInfoBT;
    }

    /**
     * 新增进货入库
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestBody PurchaseAddRequest request) {
        boolean success = purchaseService.insertListAndGoods(request);
        if(success){
            String orderNumber = "JH" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
            return SUCCESS_TIP.setResultData(orderNumber);
        }
        return new ErrorTip(500, "保存失败, 请检查参数!");
    }

    /**
     * 删除进货入库
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long purchaseId) {
        boolean success = purchaseGoodsService.deleteGoodsAndListRecord(purchaseId);
        if(success){
            return SUCCESS_TIP;
        }
        return new ErrorTip(500, "删除失败, 请检查参数!");
    }

    /**
     * 修改进货入库
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Purchase purchase) {
        purchaseService.updateById(purchase);
        return SUCCESS_TIP;
    }

    /**
     * 进货入库详情
     */
    @RequestMapping(value = "/detail/{purchaseId}")
    @ResponseBody
    public Object detail(@PathVariable("purchaseId") Integer purchaseId) {
        return purchaseService.selectById(Long.valueOf(purchaseId));
    }
}
